/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ruisitech.bi.ext.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruisitech.bi.ext.entity.SysUser;
import com.ruisitech.bi.entity.frame.User;
import com.ruisitech.bi.ext.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SsoService {
    @Autowired
    SysUserMapper sysUserMapper;

    public User ssoValidate(String account) {
        LambdaQueryWrapper<SysUser> lambda = new LambdaQueryWrapper<>();
        lambda.eq(SysUser::getUserName, account)
                .last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lambda);
        if (sysUser == null) {
            throw new RuntimeException("账号不存在");
        }
        /**
         * {"userId":1,"staffId":"admin","loginName":"系统管理员","gender":"女","state":1,"deptCode":"002"}
         */
        User user = new User();
        user.setState(1);
        user.setStaffId(sysUser.getUserName());
        user.setGender(sysUser.getGender());
        user.setLoginName(sysUser.getUserFullname());
        user.setUserId(Integer.valueOf(sysUser.getId().intValue()));
        user.setDbName("mysql");
        user.setPassword("123456");
        return user;
    }

}
